package com.spring.nio.core.startup;

import com.google.common.collect.Maps;
import com.spring.nio.business.user.dto.UserDTO;
import com.spring.nio.common.BasicConstant;
import com.spring.nio.core.event.model.EventModelEnum;
import com.spring.nio.service.chat.frame.ChatMessageFrame;
import com.spring.nio.service.file.frame.FileMessageFrame;
import com.spring.nio.util.LocalTime;
import com.spring.nio.util.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: YSFY
 * @date: 2021-03-24 10:37
 * @pacage_name: com.spring.nio.core.startup
 * @class_name: BasicServer
 * @description: BasicServer
 */
@Slf4j
public class BasicServer {
    /**
     * 公共共享数据集合,必须强引用指向
     * */
    private static final Map<String, Object> map = new ConcurrentHashMap<>();
    public final static Lock chatLock = new ReentrantLock(true);
    public final static Lock fileLock = new ReentrantLock(true);

    /**
     * 启动基础配置服务
     */
    public static void startupBasicServer() {
        // 1、读取配置文件
        loadConfigProperties();

        // 2、设置枚举
        setEnumsType();
    }

    /**
     * 读取配置文件数据并设置进缓存
     * */
    private static void loadConfigProperties() {
        PropertiesUtil.initProperties();

        // CPU核数信息
        map.put(BasicConstant.CPU_CORE_COUNT, Runtime.getRuntime().availableProcessors());
        // 操作系统信息
        map.put(BasicConstant.OS_NAME, System.getProperty(BasicConstant.OS_NAME));
        PropertiesUtil.getInstance().stringPropertyNames().stream().forEach(param -> {
            if(!map.containsKey(param)) {
                map.put(param, PropertiesUtil.getValue(param));
            }
        });
        log.info("--> BasicServer | loadConfigProperties --> 读取配置文件[config.properties], result = success, Server print Config");

        System.out.println("/+---------- Server Properties Begin ----------+/");
        System.out.println();

        // 打印配置信息
        map.entrySet().stream().forEach(entry -> {
            System.out.println("/+-- ---> " + entry.getKey() + " = " + entry.getValue() + " ----+/");
        });
        System.out.println("/+---------- Server Properties End ----------+/");
        System.out.println();
    }

    /**
     * 设置枚举
     */
    private static void setEnumsType() {
        // 设置聊天枚举
        ChatMessageFrame.FrameType[] chatFrameTypes = ChatMessageFrame.FrameType.values();
        Map<String, ChatMessageFrame.FrameType> chatFrameTypeMap = Maps.newHashMap();
        for(ChatMessageFrame.FrameType frameType : chatFrameTypes) {
            chatFrameTypeMap.put(frameType.getBit(), frameType);
        }
        map.put(BasicConstant.CHAT_MESSAGE_FRAME_TYPE, chatFrameTypeMap);

        // 设置文件枚举
        FileMessageFrame.FrameType[] fileFrameTypes = FileMessageFrame.FrameType.values();
        Map<String, FileMessageFrame.FrameType> fileFrameTypeMap = Maps.newHashMap();
        for(FileMessageFrame.FrameType frameType : fileFrameTypes) {
            fileFrameTypeMap.put(frameType.getBit(), frameType);
        }
        map.put(BasicConstant.FILE_MESSAGE_FRAME_TYPE, fileFrameTypeMap);

        // 设置Nio事件枚举
        EventModelEnum[] eventModelEnums = EventModelEnum.values();
        Map<String, EventModelEnum> eventModelEnumMap = Maps.newHashMap();
        for(EventModelEnum eventModelEnum : eventModelEnums) {
            eventModelEnumMap.put(eventModelEnum.getName(), eventModelEnum);
        }
        map.put(BasicConstant.EVENT_TYPE, eventModelEnumMap);
    }

    /**
     * 返回缓存Map对象
     * */
    public static Map<String, Object> getMap() {
        return map;
    }
}
